const db = require("../db");
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const config = require('../config')

// 获取用户列表
exports.usersRouter = (req, res) => {
    let { query: searchName } = req.query;
    let query1
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    if (searchName) {
        query1 = `select * from users where username like '%${searchName}%'`
    } else {
        query1 = `select * from users`
    }
    db.query(query1, (err, data) => {
        if (err) { obj.meta.msg = err, obj.meta.status = 400 }
        if (data.length <= 0) { obj.meta.msg = '没有查询到数据', obj.meta.status = 400 }
        obj.meta.msg = '查询成功', obj.meta.status = 200, obj.data = data
        return res.send(obj);
    })
}

// 通过id删除用户
exports.deleteRouterById = (req, res) => {
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let id = req.params.id;
    const query1 = `DELETE FROM users WHERE id = ?`
    db.query(query1, id, (err, data) => {
        if (err) { obj.meta.msg = err, obj.meta.status = 400 }
        if (data.affectedRows != 1) { obj.meta.msg = '删除失败', obj.meta.status = 400 }
        obj.meta.msg = '删除成功', obj.meta.status = 200
        return res.send(obj);
    })
}

// 通过id查找用户
exports.inquireRouterById = (req, res) => {
    let id = req.params.id;
    const query1 = `select * from users WHERE id = ?`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, id, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length === 1) {
                var data1 = {
                    "id": results[0].id,
                    "username": results[0].username,
                    "nickname": results[0].nickname,
                    "userpic": results[0].user_pic,
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data = data1;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

// 通过id更新用户信息
exports.editUserRouter = (req, res) => {
    let id = req.params.id;
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let { username, nickname } = req.body;
    let changeValue = {
        username: username,
        nickname: nickname,
    }
    let sql1 = 'update users set ? where id=?';
    db.query(sql1, [changeValue, id], (err, results) => {
        if (err) {
            obj.meta.msg = '数据库查询错误';
            obj.meta.status = 500;
            return res.send(obj)
        } else {
            if (results.affectedRows == 1) {
                obj.meta.msg = '更新成功';
                obj.meta.status = 200;
                return res.send(obj)
            }
        }
    })
}